package com.mmlc.meimei.lichengkongjian.dialog;

import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.utils.ToastUtils;


/**
 * 日期选择
 */

public class TimeDialog extends BaseCenterFragmentDialog implements View.OnClickListener, TimePicker.OnTimeChangedListener {

    private String initDate;
    private ResponseListener<String> listener;
    private String title;
    private TimePicker timePicker;
    private TextView timeTv;

    @Override
    protected void initViews() {
        v = inflater.inflate(R.layout.dialog_time, null);
        timePicker = (TimePicker) v.findViewById(R.id.timepicker);
        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(this);
        v.findViewById(R.id.cancle).setOnClickListener(this);
        v.findViewById(R.id.commit).setOnClickListener(this);
        timeTv = (TextView) v.findViewById(R.id.time);
        if (title != null) {
            ((TextView) v.findViewById(R.id.title)).setText(title);
        }
        if(initDate!=null){
            timeTv.setText(initDate);
        }
    }


    /**
     * @param initDate -格式(1988-10-1)
     * @param listener
     * @return
     */
    public TimeDialog setInitDateAndListner(String initDate, String title, ResponseListener<String> listener) {
        this.initDate = initDate;
        this.listener = listener;
        this.title = title;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.commit:
                if(hour<13 || hour>19){
                    ToastUtils.showToast(getActivity(),"试衣时间:13:00到19:00,请重新选择");
                    return;
                }
                listener.response(initDate);
                break;
        }
        dismiss();
    }

    int hour=0;

    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        hour=hourOfDay;
        initDate=hourOfDay+":"+(minute<10? "0"+minute:minute);
        timeTv.setText(initDate);
    }
}
